<?php

class Controller_Creative extends Controller_Abstract
{
	function actionShow()
	{
		if(isset($_SESSION['infos'])){
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
		$creativemodel = new Creative();
		$this->_view['creatives'] = $creativemodel->getCreatives();
	}
	
	function actionAdd()
	{
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$name= $this->_context->name;
			$type= $this->_context->type;
			$size = $this->_context->size;
			$url = $this->_context->url;
			$html = $this->_context->html;
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (empty($type))
			{
				
			}
			if (count($errors)==0)
			{
				$cremodel = new Creative();
				$cremodel->add($name, $type, $size, $url, $html);
				$_SESSION['infos'][] = Util::getMessage(5001);
				return $this->_redirect(url('creative/show'));
			}
			else
			{
				$this->_view['name']  = $name;
				$this->_view['url'] = $url;
				$this->_view['errors'] = $errors;
				$this->_view['type'] = $type;
				$this->_view['size'] = $size;
				$this->_view['html'] = $html;
			}
		}
	}
	
	function actionEdit(){
		$id = isset($this->_context->id) ? $this->_context->id : '';
		if('' == $id){
			$_SESSION['infos'][] = Util::getMessage(1017);
			return $this->_redirect(url('creative/show'));
		}
		if($this->_context->isGET()){
			$creativemodel = new Creative();
			$creaive = $creativemodel->getCreativeById($id);
			if($creaive){
				$this->_view['name']  = $creaive['name'];
				$this->_view['url'] = $creaive['url'];
				$this->_view['errors'] = $errors;
				$this->_view['type'] = $creaive['type'];
				$this->_view['size'] = $creaive['size'];
				$this->_view['html'] = $creaive['html'];
				$this->_view['id'] = $creaive['id'];
			}
			else{
				$_SESSION['infos'][] = Util::getMessage(5003);
				return $this->_redirect(url('creative/show'));
			}
		}elseif ($this->_context->isPOST()){
			$errors = array();
			$name= $this->_context->name;
			$type= $this->_context->type;
			$size = $this->_context->size;
			$url = $this->_context->url;
			$html = $this->_context->html;
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			if (empty($type))
			{
				
			}
			if (empty($size))
			{
				
			}
			if (empty($url))
			{
				$errors[] = Util::getMessage(5002);
			}
			if (count($errors)==0)
			{
				$cremodel = new Creative();
				$result = $cremodel->update($name, $type, $size, $url, $html, $id);
				if($result){
					$_SESSION['infos'][] = Util::getMessage(5004);
					return $this->_redirect(url('creative/show'));
				}
			}
			else
			{
				$this->_view['name']  = $name;
				$this->_view['url'] = $url;
				$this->_view['errors'] = $errors;
				$this->_view['type'] = $type;
				$this->_view['size'] = $size;
				$this->_view['html'] = $html;
				$this->_view['id'] = $id;
			}
		}
	}
	
	function actionDelete(){
		$id = isset($this->_context->id) ? $this->_context->id : '';
		if('' == $id){
			$_SESSION['infos'][] = Util::getMessage(1017);
			return $this->_redirect(url('creative/show'));
		}
		$creativemodel = new Creative();
		$reult = $creativemodel->delete($id);
		if($reult){
			$_SESSION['infos'][] = Util::getMessage(5005);
			return $this->_redirect(url('creative/show'));
		}
	}
}
?>